package com.by.hellosecurityoauth2.controller;

import com.by.hellosecurityoauth2.controller.request.RoleAuthorityRequestBean;
import com.by.hellosecurityoauth2.domain.TRole;
import com.by.hellosecurityoauth2.domain.TRoleAuthority;
import com.by.hellosecurityoauth2.service.RoleService;
import com.by.hellosecurityoauth2.controller.request.RoleRequestBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 白羽
 * @Date 2022-12-13 09:15:55
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {
    /**
     * 服务对象
     */
    private RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "根据id获取角色信息")
    @PreAuthorize("hasAuthority('user:role:get')")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public TRole getRoleById(@PathVariable("id") Long id) {
        return roleService.findById(id);
    }

    @ApiOperation(value = "根据关键字查询角色列表")
    @PreAuthorize("hasAuthority('user:role:get')")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public Page<TRole> list(@RequestParam @Parameter(description = "页数，从1开始", required = true) int page,
                            @RequestParam @Parameter(description = "每页的条数", required = true) int limit,
                            HttpServletRequest request) {

        return roleService.list(page - 1, limit);

    }

    @Operation(summary = "创建角色信息")
    @PreAuthorize("hasAuthority('user:role:create')")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public TRole createRole(@RequestBody RoleRequestBean bean, HttpServletRequest request) {
        return roleService.create(bean);
    }

    @Operation(summary = "根据id修改角色信息")
    @PreAuthorize("hasAuthority('user:role:update')")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public TRole updateRoleById(@PathVariable("id") Long id, @RequestBody RoleRequestBean bean, HttpServletRequest request) {
        return roleService.updateById(id, bean);
    }

    @Operation(summary = "根据id删除角色信息")
    @PreAuthorize("hasAuthority('user:role:delete')")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public void deleteRoleById(@PathVariable("id") Long id, HttpServletRequest request) {
        roleService.deleteById(id);
    }

    @Operation(summary = "根据角色id批量配置角色权限信息")
    @PreAuthorize("hasAuthority('user:role:update')")
    @RequestMapping(value = "/authority/{id}", method = RequestMethod.PUT)
    public List<TRoleAuthority> updateRoleAuthority(@PathVariable("id") Long id, @RequestBody List<RoleAuthorityRequestBean> bean, HttpServletRequest request) {
        return roleService.updateRoleAuthority(id, bean);
    }

    @Operation(summary = "根据角色i获取角色权限信息")
    @PreAuthorize("hasAuthority('user:role:get')")
    @RequestMapping(value = "/authority/{id}", method = RequestMethod.GET)
    public List<TRoleAuthority> getRoleAuthorityByRoleId(@PathVariable("id") Long id, HttpServletRequest request) {
        return roleService.getRoleAuthorityByRoleId(id);
    }

}